package com.digitalchina.lxnsy.adapter;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.digitalchina.lxnsy.R;
import com.digitalchina.lxnsy.entity.ProductTypeVO;

import java.util.List;

public class FirstAdapter extends BaseAdapter {

    private List<ProductTypeVO> mDatas;
    private Context mContext;
    private final LayoutInflater layoutInflater;
    private int mPosition;

    public FirstAdapter(Context mContext, List<ProductTypeVO> mDatas) {
        this.mContext = mContext;
        this.mDatas = mDatas;
        layoutInflater = LayoutInflater.from(this.mContext);
    }

    @Override
    public int getCount() {
        return mDatas.size();
    }

    @Override
    public Object getItem(int position) {
        return mDatas.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView == null) {
            viewHolder = new ViewHolder();
            convertView = layoutInflater.inflate(R.layout.item_mall_type_first, parent, false);
            viewHolder.textView = (TextView) convertView.findViewById(R.id.type_first_tv_name);
            viewHolder.imageView = (ImageView) convertView.findViewById(R.id.type_first_iv_icon);
            viewHolder.linearLayout = (LinearLayout) convertView.findViewById(R.id.type_first_layout);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        if (mPosition == position) {
            viewHolder.linearLayout.setBackgroundColor(mContext.getResources().getColor(R.color.app_color));
        } else {
            viewHolder.linearLayout.setBackgroundColor(Color.parseColor("#404040"));
        }

        switch (position % 5) {
            case 0:
//                viewHolder.imageView.setBackgroundResource(R.drawable.ic_data_type1);
                viewHolder.imageView.setImageResource(R.drawable.ic_data_type1);
                break;
            case 1:
//                viewHolder.imageView.setBackgroundResource(R.drawable.ic_data_type2);
                viewHolder.imageView.setImageResource(R.drawable.ic_data_type2);
                break;
            case 2:
//                viewHolder.imageView.setBackgroundResource(R.drawable.ic_data_type4);
                viewHolder.imageView.setImageResource(R.drawable.ic_data_type4);
                break;
            case 3:
//                viewHolder.imageView.setBackgroundResource(R.drawable.ic_data_type3);
                viewHolder.imageView.setImageResource(R.drawable.ic_data_type3);
                break;
            case 4:
//                viewHolder.imageView.setBackgroundResource(R.drawable.ic_data_type2);
                viewHolder.imageView.setImageResource(R.drawable.ic_data_type2);
                break;
            case 5:
//                viewHolder.imageView.setBackgroundResource(R.drawable.ic_data_type1);
                viewHolder.imageView.setImageResource(R.drawable.ic_data_type1);
                break;
        }
        viewHolder.textView.setText(mDatas.get(position).getName());

        return convertView;
    }

    public void setSelectPosition(int position) {
        mPosition = position;
    }

    public ProductTypeVO getCurrentPositionItem() {
        return mDatas.get(mPosition);
    }

    private static class ViewHolder {
        TextView textView;
        LinearLayout linearLayout;
        ImageView imageView;
    }

}
